package com.aiss.ws.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiss.ws.R;
import com.aiss.ws.utils.Command;
import com.aiss.ws.utils.Futil;
import com.aiss.ws.utils.Url;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by chenqiang on 16/5/17.
 */
@EActivity(R.layout.activity_regist)
public class RegistActivity extends BaseActivity {

    private Context context = this;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==Command.HANDLECODE2){


                try {
                    JSONObject obj = (JSONObject) msg.obj;
                    JSONObject jsonObject = obj.getJSONObject("return_data");
                    String member_id = jsonObject.getString("member_id");
                    String session_key = jsonObject.getString("session_key");
                    String name = jsonObject.getString("name");
                    Futil.saveValue(context,Command.MEMBER_ID,member_id,2);
                    Futil.saveValue(context,Command.SESSION_KEY,session_key,2);
                    Futil.saveValue(context,Command.NAME,name,2);


                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if(img_press.getVisibility()== View.VISIBLE){
                    Futil.saveValue(context, Command.PASSWORD,password.getText().toString(),2);
                }
                finish();

            }
        }
    };

    private static Activity act;

    @ViewById(R.id.user)
    EditText user;

    @ViewById(R.id.password)
    EditText password;

    @ViewById(R.id.title)
    TextView title_tv;

    @ViewById(R.id.img)
    ImageView img;

    @ViewById(R.id.img_press)
    ImageView img_press;


    @Click(R.id.back)
    void setBack(){
        finish();
    }

    @Click(R.id.going_login)
    void going_login(){
        LoginActivity_.intent(this).start();
    }

    @AfterViews
    void init(){
        act = this;
        title_tv.setText("登陆");

        if(!Futil.getValue(context,Command.PASSWORD,2).toString().equals("")){
            user.setText(Futil.getValue(context,Command.NAME,2).toString());
            password.setText(Futil.getValue(context,Command.PASSWORD,2).toString());
            img_press.setVisibility(View.VISIBLE);
            img.setVisibility(View.GONE);
        }
    }

    /**
     * 立即登录
     */
    @Click(R.id.next_button)
    void next_button(){
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("type","login");
        hashMap.put("name",user.getText().toString());
        hashMap.put("password",password.getText().toString());
        Futil.xutils(Url.LOGIN,hashMap,handler,Command.HANDLECODE2);
    }

    public static Activity getActivity(){
        return act;
    }

    /**
     * 记住密码
     */
    @Click(R.id.remeber)
    synchronized void remeber(){
        if(img_press.getVisibility()== View.VISIBLE){
            img_press.setVisibility(View.GONE);
            img.setVisibility(View.VISIBLE);
        }else{
            img_press.setVisibility(View.VISIBLE);
            img.setVisibility(View.GONE);
        }
    }


}
